/*
 * @Author: yanjikun
 * @Date: 2022-01-12 11:04:05
 * @LastEditors: yanjikun
 * @LastEditTime: 2022-05-17 14:26:28
 * @Description: file content
 */

const toString = Object.prototype.toString

export const is = (val: unknown, type: string) => {
  return toString.call(val) === `[object ${type}]`
}
export const isDef = <T = unknown>(val?: T): val is T => {
  return typeof val !== 'undefined'
}

export const isUndef = <T = unknown>(val: T): val is T => {
  return !isDef(val)
}
export const isNull = (val: unknown): val is null => {
  return val === null
}

export const isNullOrUndef = (val: unknown): val is null | undefined => {
  return isUndef(val) || isNull(val)
}

export const isObject = (val: unknown): val is Record<any, any> => {
  return val !== null && is(val, 'Object')
}

export const isArray = (val: unknown): val is Array<any> => {
  return Array.isArray(val)
}

export const isFunction = (val: unknown): val is Function => {
  return typeof val === 'function'
}

export const isString = (val: unknown): val is string => {
  return is(val, 'String')
}

export const isNumber = (val: unknown): val is number => {
  return is(val, 'Number')
}

export function isUrl(path: string): boolean {
  const reg = /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/
  return reg.test(path)
}
